import { checkIfLogin } from '../../utils/check'
import { queryUserOrder, cancelOrder } from '../../services/order'
import { queryVehicleDetail } from '../../services/vehicle'
import { getPackageDetailInfo } from '../../services/inspect-project'
import Toast from '@vant/weapp/toast/toast'
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    active:'all',
    orderList:[],
    showDialogConfirm:false,
    dialogMsg:"",
    currentItem:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    checkIfLogin()
    if(!options){
      return
    }

    let tabIndex = ""
    tabIndex = options.tabIndex
    this.setData({
      active: tabIndex ? tabIndex : 'all'
    })
    this.checkStatus(tabIndex)
  },

  onShow(){
    checkIfLogin()
    this.checkStatus(this.data.active)
  },

  checkStatus(tabIndex){
    let status = ""
    let data = {}
    if( tabIndex == 'toBeProcessed' ){
      status = 1
    } else if( tabIndex == 'processing' ){
      status = 2
    } else if( tabIndex == 'handled' ){
      status = 3
    }

    if(status){
      data = {
        status
      }
    }
    this.getOrderList(data)
  },


  async getOrderList(data){
    let orderRes = await queryUserOrder(data)
    if(orderRes.code === 200){
      this.setData({
        orderList: orderRes.rows
      })
    }
  },

  onChange(event){
    let tabIndex = event.detail.name
    this.checkStatus(tabIndex)
    this.setData({
      active: event.detail.name
    })
  },

  handleViewDetail(event){
    let currentItem = event.detail
    let currentId = currentItem.id
    wx.redirectTo({
      url: `/pages/order-detail/order-detail?orderId=${currentId}`,
    })
  },

  handleViewReport(event){
    let orderId = event.detail?.id
    wx.navigateTo({
      url: `/pages/report-detail/report-detail?orderId=${orderId}`,
    })
  },

  async handleAppointmentAgain(event){
    let currentItem = event.detail
    const { appointPackageId, appointPackageName, plateNo, stationId, stationName, vehicleId  } = currentItem

    let vehicleRes = await queryVehicleDetail(vehicleId)
    let vehicleType = ""
    if(vehicleRes.code === 200){
      vehicleType = vehicleRes.data.vehicleType
    }

    let detectProjectName = ""
    let packageInfoRes = await getPackageDetailInfo(appointPackageId)
    if(packageInfoRes.code === 200){
      detectProjectName = packageInfoRes.data.detectProjectName
    }

    let glabalInfo = {
      appointPackageId,
      appointPackageName,
      detectProjectName,
      plateNo,
      stationId,
      stationName,
      vehicleId,
      vehicleType
    }
    app.globalData.globalInfo = glabalInfo
    wx.navigateTo({
      url: '/pages/inspection-project/inspection-project',
    })
  },

  async handleEditOrder(event){
    let currentItem = event.detail
    const { id, appointDate, appointPackageId, appointPackageName, appointTime, plateNo, stationId, stationName, vehicleId  } = currentItem

    let vehicleRes = await queryVehicleDetail(vehicleId)
    console.log("vehicleRes>>>", vehicleRes)
    let vehicleType = ""
    if(vehicleRes.code === 200){
      vehicleType = vehicleRes.data.vehicleType
    }

    let detectProjectName = ""
    let packageInfoRes = await getPackageDetailInfo(appointPackageId)
    if(packageInfoRes.code === 200){
      detectProjectName = packageInfoRes.data.detectProjectName
    }

    let glabalInfo = {
      id,
      appointDate,
      appointPackageId,
      appointPackageName,
      appointTime,
      detectProjectName,
      plateNo,
      stationId,
      stationName,
      vehicleId,
      vehicleType
    }
    app.globalData.globalInfo = glabalInfo
    wx.navigateTo({
      url: '/pages/inspection-project/inspection-project',
    })
  },

  handleCancel(event){
    let currentItem = event.detail
    this.setData({
      showDialogConfirm: true,
      dialogMsg: "是否确认取消预约?",
      currentItem,
    })
  },

  async handleConfirm(){
    let info = {
      id: this.data.currentItem.id
    }
    let cancelRes = await cancelOrder(info)
    if(cancelRes.code === 200){
      Toast.success('取消成功')
      this.checkStatus(this.data.active)
    }
  },


})